﻿<script setup>
import { reactive, ref } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { useAuthStore } from '../stores/auth';

const router = useRouter();
const authStore = useAuthStore();

const form = reactive({
  username: import.meta.env.VITE_DEFAULT_USERNAME || '',
  pwd: import.meta.env.VITE_DEFAULT_PASSWORD || '123456',
  realName: import.meta.env.VITE_DEFAULT_REAL_NAME || '',
  age: 20,
  email: import.meta.env.VITE_DEFAULT_EMAIL || '',
  stuNum: import.meta.env.VITE_DEFAULT_STU_NUM || '',
  className: import.meta.env.VITE_DEFAULT_CLASS || '',
});

const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  pwd: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  realName: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
  stuNum: [{ required: true, message: '请输入学号', trigger: 'blur' }],
  className: [{ required: true, message: '请输入班级名称', trigger: 'blur' }],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] },
  ],
};

const loading = ref(false);
const formRef = ref();

const handleSubmit = () => {
  if (!formRef.value) return;
  formRef.value.validate(async (valid) => {
    if (!valid) return;
    try {
      loading.value = true;
      await authStore.register({ ...form });
      ElMessage.success('注册成功，请使用该账号登录');
      router.push({ name: 'login' });
    } catch (error) {
      ElMessage.error(error?.message || '注册失败，请稍后重试');
    } finally {
      loading.value = false;
    }
  });
};

const goLogin = () => {
  router.push({ name: 'login' });
};
</script>

<template>
  <div class="auth-page">
    <el-card class="auth-card" shadow="always">
      <div class="auth-card__header">
        <h2>注册新用户</h2>
        <p>请填写真实的学号、姓名和班级信息以便后续验证。</p>
      </div>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px" size="large">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="建议使用学号作为用户名" clearable />
        </el-form-item>
        <el-form-item label="登录密码" prop="pwd">
          <el-input v-model="form.pwd" type="password" placeholder="建议设置为123456" show-password />
        </el-form-item>
        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入姓名" clearable />
        </el-form-item>
        <el-form-item label="学号" prop="stuNum">
          <el-input v-model="form.stuNum" placeholder="请输入学号" clearable />
        </el-form-item>
        <el-form-item label="所在班级" prop="className">
          <el-input v-model="form.className" placeholder="请输入班级名称" clearable />
        </el-form-item>
        <el-form-item label="年龄">
          <el-input-number v-model="form.age" :min="16" :max="80" />
        </el-form-item>
        <el-form-item label="电子邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱地址" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="loading" @click="handleSubmit">提交注册</el-button>
          <el-button type="default" link @click="goLogin">已有账号？立即登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<style scoped>
.auth-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f3f5ff, #e6f7ff);
  padding: 40px 20px;
}

.auth-card {
  width: 640px;
}

.auth-card__header {
  text-align: center;
  margin-bottom: 24px;
}

.auth-card__header h2 {
  font-size: 26px;
  margin-bottom: 8px;
}

.auth-card__header p {
  color: #666;
  font-size: 14px;
}
</style>
